/**********************************************************
 *
 *  Copyright (c) 2003  SeikoEpson Inc
 *  All Rights Reserved
 *
 *  File name : f_cosh.c
 *  Function  :
 *        f_sinh function returns hypublic cos's value.
 *        This file is copied from math.lib of 
 *                  CC33 tool(CC33v40.exe + CCPA45.exe, 
 *                  math of ansilib33v25 )
 *
 *  original file's Revision  :
 *      2000/02/14    first release                 M.Igarashi
 *  Revision  :
 *      2003/04/07    IrumaSoft M.Takeishi   1.st design
 *
 **********************************************************/
#include <f_math.h>
#include <f_smcvals.h>

// ALGORITHM
// 1.cosh x = ( e^x + e^-x ) / 2
//

/*  the architecture of float floating point
 *
 *   0 30         23 22                0  32 bit
 *   -----------------------------------
 *  | |   exponent  |  fraction         |
 *   -----------------------------------
 *
 *  |               |                   |
 *  |    8 bits     |    23 bits        |
 *
 *         bit    31         sign bit        (  1 bit  )
 *             30 - 23      exponent part    (  8 bits )
 *             22 -  0      fraction part    ( 23 bits )
 *
 */
 


float f_cosh(float sfX){

	long lX;
	unsigned long ulx;
	float sfX2,sfRet,sfTemp,sfTemp2;
	
	F_GETW(lX,sfX);				// get low
	F_CLR_SIGN( sfX2, sfX );	// clear sign

	ulx = lX&0x7fffffff;	// mask sign

	//  ulx > 37 ?
	if ( ulx > 0x42140000 ) {	//check area

		if ( ( ulx & 0x7fc00000 ) == 0x7fc00000 ) {
			errno = EDOM;
			return f_NAN._F;
		}

		sfTemp=sfX2 + F_LS;
		sfRet= f_exp(sfTemp);
		return sfRet;
	}else{
		sfTemp=sfX2+F_LS;
		sfTemp=f_exp(sfTemp);
		sfTemp2=-sfX2+F_LS;
		sfTemp2=f_exp(sfTemp2);
		sfRet=sfTemp+sfTemp2;
		return sfRet;
	}

}
